package com.example.hrsystem.ui.attendance;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.example.hrsystem.data.model.AttendanceRecord;
import com.example.hrsystem.data.repository.AttendanceRepository;

import java.util.Date;
import java.util.List;

/**
 * 打卡ViewModel
 * 连接Repository和UI层，处理打卡相关的UI数据逻辑
 */
public class AttendanceViewModel extends AndroidViewModel {

    private AttendanceRepository repository;
    private LiveData<List<AttendanceRecord>> allAttendanceRecords;
    private MutableLiveData<Boolean> isLoading = new MutableLiveData<>(false);
    private MutableLiveData<String> errorMessage = new MutableLiveData<>();

    /**
     * 构造方法
     */
    public AttendanceViewModel(@NonNull Application application) {
        super(application);
        repository = new AttendanceRepository(application);
        allAttendanceRecords = repository.getAllAttendanceRecords();
    }

    /**
     * 获取所有打卡记录
     */
    public LiveData<List<AttendanceRecord>> getAllAttendanceRecords() {
        return allAttendanceRecords;
    }

    /**
     * 根据员工ID获取打卡记录
     */
    public LiveData<List<AttendanceRecord>> getAttendanceRecordsByEmployeeId(String employeeId) {
        return repository.getAttendanceRecordsByEmployeeId(employeeId);
    }

    /**
     * 根据日期获取打卡记录
     */
    public LiveData<AttendanceRecord> getAttendanceRecordByDate(Date date, String employeeId) {
        return repository.getAttendanceRecordByDate(date, employeeId);
    }

    /**
     * 获取指定日期范围内的打卡记录
     */
    public LiveData<List<AttendanceRecord>> getAttendanceRecordsByDateRange(Date startDate, Date endDate, String employeeId) {
        return repository.getAttendanceRecordsByDateRange(startDate, endDate, employeeId);
    }

    /**
     * 获取未完成打卡的记录
     */
    public LiveData<List<AttendanceRecord>> getUncompletedAttendanceRecords(String employeeId) {
        return repository.getUncompletedAttendanceRecords(employeeId);
    }

    /**
     * 获取今日签到记录
     */
    public LiveData<AttendanceRecord> getTodayAttendanceRecord(String employeeId) {
        return repository.getTodayAttendanceRecord(employeeId);
    }

    /**
     * 执行签到操作
     */
    public void checkIn(String employeeId, String location) {
        isLoading.setValue(true);
        errorMessage.setValue(null);

        try {
            AttendanceRecord record = new AttendanceRecord();
            record.setEmployeeId(employeeId);
            record.setCheckInTime(new Date());
            record.setCheckInLocation(location);
            record.setAttendanceType("正常");
            record.setCompleted(false);

            repository.insert(record);
        } catch (Exception e) {
            errorMessage.setValue("签到失败：" + e.getMessage());
            e.printStackTrace();
        } finally {
            isLoading.setValue(false);
        }
    }

    /**
     * 执行签退操作
     */
    public void checkOut(String employeeId, String location) {
        isLoading.setValue(true);
        errorMessage.setValue(null);

        try {
            // 获取今日签到记录
            AttendanceRecord todayRecord = repository.getTodayAttendanceRecordSync(employeeId);
            if (todayRecord != null) {
                todayRecord.setCheckOutTime(new Date());
                todayRecord.setCheckOutLocation(location);
                todayRecord.setCompleted(true);

                // 检查是否迟到或早退
                // 这里可以根据公司考勤规则判断打卡类型
                todayRecord.setAttendanceType("正常");

                repository.update(todayRecord);
            } else {
                errorMessage.setValue("未找到今日签到记录，无法签退");
            }
        } catch (Exception e) {
            errorMessage.setValue("签退失败：" + e.getMessage());
            e.printStackTrace();
        } finally {
            isLoading.setValue(false);
        }
    }

    /**
     * 获取加载状态
     */
    public LiveData<Boolean> getIsLoading() {
        return isLoading;
    }

    /**
     * 获取错误信息
     */
    public LiveData<String> getErrorMessage() {
        return errorMessage;
    }
}